<?php
class pro_mitra_model extends MY_Model {
	var $model = 'pro_mitra';
	
	function GetJSON()
	{

		$ls_sql	= "	SELECT	id_mitra,
							kode_mitra,
							ktp,
							situ,
							siup,
							npwp,
							nama_pemilik,
							nama_toko_mitra,
							alamat_lengkap,
							no_telp,
							no_hand,
							status_mitra,
							no_kontrak,
							TO_CHAR(awal_kerja,'DD/MM/YYYY') awal_kerja,
							TO_CHAR(akhir_kerja,'DD/MM/YYYY') akhir_kerja,
							fee_mitra,
							no_rekening,
							nama_bank,
							atas_nama
					FROM	pro_mitra
					WHERE	UPPER(status_mitra) = 'BELUM AKTIF' AND
							(UPPER(kode_mitra) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(ktp) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_pemilik) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_toko_mitra) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(alamat_lengkap) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(no_telp) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(no_hand) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(status_mitra) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(no_kontrak) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(awal_kerja) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(akhir_kerja) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(fee_mitra) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(no_rekening) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(nama_bank) LIKE '%".strtoupper(post('keyword'))."%'
							OR UPPER(atas_nama) LIKE '%".strtoupper(post('keyword'))."%')
							ORDER BY ".(post('sort')=='' ? 'kode_mitra' : post('sort'))." ".(post('order')=='' ? 'asc' : post('order'));
					
		return json_encode( $this->paging($ls_sql) );
	}
		
	function Add($arr_data){
		$this->db->insert('pro_mitra', $arr_data);
	}
		
	
	function Update($arr_data, $arr_rule){
		$this->db->update('pro_mitra', $arr_data, $arr_rule);
	}
	
	function GetSingle($as_kode_mitra){
		$result = $this->db->query("	SELECT	id_mitra,
												kode_mitra,
												ktp,
												situ,
												siup,
												npwp,
												nama_pemilik,
												nama_toko_mitra,
												alamat_lengkap,
												no_telp,
												no_hand,
												status_mitra,
												no_kontrak,
												TO_CHAR(awal_kerja,'DD/MM/YYYY') as awal_kerja,
												TO_CHAR(akhir_kerja,'DD/MM/YYYY') as akhir_kerja,
												fee_mitra,
												no_rekening,
												nama_bank,
												atas_nama,
												keterangan
										FROM	pro_mitra
										WHERE kode_mitra = '$as_kode_mitra'");
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}
	
	function GetSingleView($as_kode_mitra)
	{
		$result = $this->db->query("	SELECT	id_mitra,
												kode_mitra,
												ktp,
												situ,
												siup,
												npwp,
												nama_pemilik,
												nama_toko_mitra,
												alamat_lengkap,
												no_telp,
												no_hand,
												status_mitra,
												no_kontrak,
												TO_CHAR(awal_kerja,'DD/MM/YYYY') as awal_kerja,
												TO_CHAR(akhir_kerja,'DD/MM/YYYY') as akhir_kerja,
												fee_mitra,
												no_rekening,
												nama_bank,
												atas_nama,
												keterangan
										FROM	pro_mitra
										WHERE kode_mitra = '$as_kode_mitra'");
		if($result->num_rows() == 1){
			return $result->row_array();
		}else{
			return NULL;
		}
	}

	function DeleteSingle($arr_rule)
	{
		try {
			if(	intval($this->get_data("SELECT COUNT(1) FROM pro_mitra WHERE kode_mitra = '".$arr_rule['kode_mitra']."'"))<=0)
			{
				return $this->error_delete.' [id: '.$arr_rule['kode_mitra']."]<br />";
			}else{
				$this->db->delete('pro_mitra', array('kode_mitra' => $arr_rule['kode_mitra']));
				return '';
			}
		} catch(Exception $e) {
			return $e->getMessage();
		}
	}
}
?>